package com.tbgov.interceptor;


import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

/**
 * 全局异常处理
 *
 * @author he.hb
 */
@ControllerAdvice
@Slf4j
public class ApiExceptionHandler {

    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
        String message = Optional.ofNullable(e.getMessage()).orElse("空指针异常");
        log.info("全局捕捉业务异常:" + message, e);
        return ResponseEntity.badRequest().body(message);
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<String> exceptionHandler(Exception e) {
        String message = Optional.ofNullable(e.getMessage()).orElse("空指针异常");
        log.info("全局捕捉未知异常！原因是:" + message, e);
        return ResponseEntity.badRequest().body(message);
    }

}